<?php
namespace app\app\util;
use think\cache\driver\Redis;
use think\Request;

define('POINT','Answer_point');

class PointUtils{

    //创建积分数
    public static function foundUserPoint($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        //创建用户
        return $redis->zadd(POINT,$number,$user_id);
    }

    //获取积分数
    public static function getUserPoint($user_id,$number=0)
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        if($number == 0){
            return $redis->zscore(POINT,$user_id);
        }else{
            $result = $redis->zincrby(POINT,$number,$user_id);
            return $result;
        }
    }

    //获取指定范围排名
    public static function getPointRank($start,$end,$type='')
    {
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        return $redis->zrevrange(POINT,$start,$end,$type);
    }


    /**
     * 获取用户排名
     * @param $user_id
     */
    public static function getUserRank($user_id){
        $redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
        return $redis->zrevrank(POINT,$user_id);
    }
}